﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity.ObjectBuilder;

namespace WDA.Core
{
   public static class StrategyContainerExtensionMethods
   {
      public static void AddNewStrategy<StrategyType>(this IUnityContainer container, UnityBuildStage buildStage)
         where StrategyType : IBuilderStrategy, new()
      {
         IBuilderStrategy strategy = new StrategyType();
         container.AddExtension(new StrategyContainerExtension(strategy, buildStage));
      }
   }

   internal class StrategyContainerExtension : UnityContainerExtension
   {      
      private readonly IBuilderStrategy _strategy;
      private readonly UnityBuildStage _buildStage;

      internal StrategyContainerExtension(IBuilderStrategy strategy, UnityBuildStage buildStage)
      {
         _strategy = strategy;
         _buildStage = buildStage;
      }

      protected override void Initialize()
      {
         Context.Strategies.Add(_strategy, _buildStage);
      }
   }
}
